import type { VideoInfo, FolderInfo, UploadItem } from '@/types/resource'
import http from './http'
import { apiMsgProp } from '@/utils/global'

const ResourceService = {
    // 获取视频资源
    videoInfo: async (id: number): Promise<VideoInfo> => {
        const res = await http.get(`/course/resource/video/${id}/info`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function videoSave 保存视频资源
     * @param data 视频信息
     */
    videoSave: async (data: VideoInfo): Promise<void> => {
        const res = await http.post(`/course/resource/video/save`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    /**
     * @function videoUpdate 更新视频进度
     * @param id 视频资源 id
     * @param student_id 学生 id
     * @param position 视频播放位置（单位：秒）
     * @param duration 视频总时长（单位：秒）
     * @param course_id 课程 id
     * @param unit_id 单元 id
     * @param start_time 开始观看时间，格式：YYYY-MM-DD HH:mm:ss
     */
    videoUpdate: async (
        id: number,
        data: {
            student_id: number
            position: number
            duration: number
            course_id: number
            unit_id: number
            start_time: string
        },
    ): Promise<void> => {
        const res = await http.post(`/course/resource/video/${id}/save-progress`, data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
    },

    // 获取单元资料夹
    folderInfo: async (id: number): Promise<FolderInfo> => {
        const res = await http.get(`/course/resource/folder/tree?root_directory_id=${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function folderSave 保存文件夹资源
     * @param data 文件夹资源信息
     */
    folderSave: async (data: FolderInfo): Promise<void> => {
        const res = await http.post(`/course/resource/folder/save`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },

    // 删除文件资源
    deleteFolder: async (id: number): Promise<void> => {
        const res = await http.delete(`/course/resource/folder/del/${id}`)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },

    /**
     * @function fileInfo 获取文件资源
     * @param id 文件 id
     * @returns FileInfo 文件信息
     */
    fileInfo: async (id: number): Promise<FolderInfo> => {
        const res = await http.get(`/course/resource/file/${id}/info`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data
    },
    /**
     * @function fileSave 保存文件资源
     * @param data 文件信息
     */
    fileSave: async (data: FolderInfo): Promise<void> => {
        const res = await http.post(`/course/resource/file/save`, data)
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
    // 下载文件夹资源
    downloadFolder: async (id: number): Promise<string> => {
        const res = await http.get(`/course/resource/folder/download/${id}`)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
        return res.data.data.file_url
    },
    // 上传文件
    upload: async (data: FormData): Promise<UploadItem> => {
        const res = await http.post(`/course/resource/upload`, data, {
            headers: {
                'Content-Type': 'multipart/form-data',
            },
            timeout: undefined,
        })
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
        return res.data.data
    },
    /**
     * @function videoSort 资源排序
     * @param data 资源排序信息
     */
    videoSort: async (data: { id: number; sort_order: number }[]): Promise<void> => {
        const res = await http.post(`/course/unit/sortVideo`, data)
        if (res.data.code !== 200) {
            apiMsgProp(res.data.code, res.data.message)
            return Promise.reject(res.data)
        }
    },
    /**
     * @function deleteByType 根据类型删除资源
     * @param id 资源id
     * @param type 资源类型 可选值：video、file、folder、quiz、report
     */
    deleteByType: async (id: number, type: string): Promise<void> => {
        const res = await http.delete(`/course/resource/${id}/delByType`, { data: { type } })
        apiMsgProp(res.data.code, res.data.message)
        if (res.data.code !== 200) return Promise.reject(res.data)
    },
}

export default ResourceService
